package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.bean.AddVisitVideoResponse
import com.gexiaobao.pigeon.app.model.bean.BobInfoResponse
import com.gexiaobao.pigeon.app.model.bean.CleaningResponse
import com.gexiaobao.pigeon.app.model.bean.FeatherColorResponse
import com.gexiaobao.pigeon.app.model.bean.GetVideoListResponse
import com.gexiaobao.pigeon.app.model.bean.InfoByRingSnResponse
import com.gexiaobao.pigeon.app.model.bean.LoftAllInfoResponse
import com.gexiaobao.pigeon.app.model.bean.LoftMemberResponse
import com.gexiaobao.pigeon.app.model.bean.OrgMatchResponse
import com.gexiaobao.pigeon.app.model.bean.OrganizerDeviceResponse
import com.gexiaobao.pigeon.app.model.bean.OssStsResponse
import com.gexiaobao.pigeon.app.model.bean.PrintVisitResponse
import com.gexiaobao.pigeon.app.model.bean.ReceiptPigeonResponse
import com.gexiaobao.pigeon.app.model.bean.RingSnResponse
import com.gexiaobao.pigeon.app.model.bean.ShedPigeonListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.VisitMemberListResponse
import com.gexiaobao.pigeon.app.model.param.AddLoftAgentParams
import com.gexiaobao.pigeon.app.model.param.EditVisitVideoParam
import com.gexiaobao.pigeon.app.model.param.RingSnParams
import com.gexiaobao.pigeon.app.model.param.ShedRingInfoParams
import com.gexiaobao.pigeon.app.model.param.UpDateLoftPigeonParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.IntObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.RequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/4/12 11:31
 * @Describe    :
 */
class ShedPigeonViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private val pageSize: Int = 20

    val pigeonSex = StringObservableField()//赛鸽性别
    val pigeonFeatherColor = StringObservableField()//羽色
    val pigeonEyePattern = StringObservableField()//赛鸽眼沙
    val pigeonFeatherColorPosition = IntObservableField()//羽色下标

    /** 配对 */
    var ringSn = MutableLiveData<ResultState<RingSnResponse>>()
    fun geyRingSn(body: RingSnParams) {
        request({ apiService.getRing(body) }, ringSn)
    }

    /** 获取羽色列表 */
    var featherColor: MutableLiveData<ListDataUiState<FeatherColorResponse.ColorList>> = MutableLiveData()
    fun getFeatherColorApp() {
        request({ apiService.getFeatherColorApp() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isEmpty = it.isEmpty(), listData = it.list
            )
            featherColor.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(isSuccess = false, errMessage = it.msg, listData = arrayListOf<FeatherColorResponse.ColorList>())
            featherColor.value = listDataUiState
        })
    }

    /** 获取清棚中的清棚 */
    var getCleanResult: MutableLiveData<CleaningResponse> = MutableLiveData()
    fun getCleaning(orgId: Int) {
        request({ apiService.getCleaning(orgId) }, {
            getCleanResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取sts */
    var ossStsResult: MutableLiveData<OssStsResponse> = MutableLiveData()
    fun getOssSts() {
        request({ apiService.getOssSts() }, {
            ossStsResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 开始配对 */
    var startPairResult: MutableLiveData<Any> = MutableLiveData()
    fun startPair(body: RequestBody) {
        request({ apiService.startPair(body) }, {
            startPairResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 获取足环信息 */
    var infoByRingSnResult: MutableLiveData<InfoByRingSnResponse> = MutableLiveData()
    fun getInfoByRingSn(ringSn: String, raceId: Int) {
        request({ apiService.getInfoByRingSn(ringSn, raceId) }, {
            infoByRingSnResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取用户所属公棚列表 */
    var BobInfoResult: MutableLiveData<BobInfoResponse> = MutableLiveData()
    fun getLoftOrgListByUser() {
        request({ apiService.getLoftOrgListByUser(1, 999999) }, {
            BobInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取赛鸽详情（公棚） */
    var shedPigeonDetailResult: MutableLiveData<ShedPigeonListResponse.ListData> = MutableLiveData()
    fun getLoftPigeonDetail(id: String) {
        request({ apiService.getLoftPigeonDetail(id) }, {
            shedPigeonDetailResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 删除赛鸽（公棚） */
    var deleteLoftPigeonResult: MutableLiveData<UiState> = MutableLiveData()
    fun deleteLoftPigeon(id: String) {
        request({ apiService.deleteLoftPigeon(id) }, {
            val deleteState = UiState(isSuccess = true)
            deleteLoftPigeonResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = true, errorMsg = it.msg)
            deleteLoftPigeonResult.value = deleteState
        })
    }

    /** 获取公棚所有下拉信息 */
    var allInfoResult: MutableLiveData<LoftAllInfoResponse> = MutableLiveData()
    fun getAddPigeonLoftAllInfoListByOrg() {
        request({ apiService.getAddPigeonLoftAllInfoListByOrg() }, {
            allInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 新增代理人 */
    var newAgentResult: MutableLiveData<Any> = MutableLiveData()
    fun addLoftAgent(param: AddLoftAgentParams) {
        request({ apiService.addLoftAgent(param) }, {
            newAgentResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取用户所属公棚下会员列表 */
    var loftMemberResult: MutableLiveData<LoftMemberResponse> = MutableLiveData()
    fun getLoftMemberListByUserIdAndOrgId(orgId: String, raceId: Int) {
        request({ apiService.getLoftMemberListByUserIdAndOrgId(orgId, raceId) }, {
            loftMemberResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取公棚下会员 */
    var memberListResult: MutableLiveData<Any> = MutableLiveData()
    fun getLoftMemberList(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getLoftMemberList(pageNo, pageSize, "", "", "") }, {

        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取公棚下赛鸽 */
    var pigeonListResult: MutableLiveData<ListDataUiState<ShedPigeonListResponse.ListData>> = MutableLiveData()
    fun getLoftPigeonList(isRefresh: Boolean, member: String, ringId: String, raceId: Int, loftPicIs: Int, ringSnIs: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getLoftPigeonList(pageNo, pageSize, member, ringId, raceId, loftPicIs, ringSnIs) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list
            )
            pigeonListResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = isRefresh, listData = arrayListOf<ShedPigeonListResponse.ListData>()
            )
            pigeonListResult.value = listDataUiState
        })
    }

    /** 退出登录 */
    val logOutResult: MutableLiveData<UiState> = MutableLiveData()
    fun loginOut() {
        request({ apiService.loginOut() }, {
            val deleteState = UiState(isSuccess = true)
            logOutResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            logOutResult.value = deleteState
        })
    }

    /** 更新赛鸽信息（公棚） */
    val upDateLoftPigeonResult: MutableLiveData<UiState> = MutableLiveData()
    fun upDateLoftPigeon(param: UpDateLoftPigeonParam) {
        val imgFileBody = param.loftPic
        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["id"] = param.id.toString()
        hashMap["memberId"] = param.memberId.toString()
        hashMap["ringId"] = param.ringId
        hashMap["ringSn"] = param.ringSn
        hashMap["featherColor"] = param.featherColor.toString()
        hashMap["featherColorId"] = param.featherColor.toString()
        hashMap["agentId"] = param.agentId.toString()
        hashMap["sex"] = param.sex.toString()
        hashMap["eyePattern"] = param.eyePattern.toString()
        hashMap["liveIs"] = param.liveIs.toString()
        hashMap["pigeonDesc"] = param.pigeonDesc

        if (imgFileBody != null) {
            fileList["loftPic"] = imgFileBody
        } else {
            hashMap["loftPic"] = ""
        }
        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.upDateLoftPigeon(body) }, {
            val deleteState = UiState(isSuccess = true)
            upDateLoftPigeonResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            upDateLoftPigeonResult.value = deleteState
        })
    }


    /** 添加和修改赛鸽 */
    val createPigeon: MutableLiveData<UiState> = MutableLiveData()
    fun createPigeon(bean: ShedRingInfoParams) {
        val imgFileBody = bean.bodyPic
        val imgFileFly = bean.featherPic
        val imgFileLeft = bean.leftHead
        val imgFileRight = bean.rightHeadPic
        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["fosterName"] = bean.fosterName
        hashMap["ringId"] = bean.ringId
        hashMap["memberId"] = bean.memberId.toString()
        hashMap["wantedOrgId"] = bean.wantedOrgId.toString()
        hashMap["sex"] = bean.sex.toString()
        hashMap["featherColor"] = bean.featherColor.toString()
        hashMap["eyePattern"] = bean.eyePattern.toString()

        if (imgFileBody != null) {
            fileList["bodyPic"] = imgFileBody
        } else {
            hashMap["bodyPic"] = ""
        }
        if (imgFileFly != null) {
            fileList["featherPic"] = imgFileFly
        } else {
            hashMap["featherPic"] = ""
        }
        if (imgFileLeft != null) {
            fileList["leftHead"] = imgFileLeft
        } else {
            hashMap["leftHead"] = ""
        }
        if (imgFileRight != null) {
            fileList["rightHeadPic"] = imgFileRight
        } else {
            hashMap["rightHeadPic"] = ""
        }
        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.createShedPigeon(body) }, {
            val createResult = UiState(isSuccess = true)
            createPigeon.value = createResult
            eventViewModel.toRefreshList.value = true
        }, {
            val createResult = it.errorLog?.let { it1 -> UiState(isSuccess = false, errorMsg = it1) }
            createPigeon.value = createResult
        })
    }

    /** 修改赛鸽（附带图片） */
    val modifyPigeonPicResult: MutableLiveData<UiState> = MutableLiveData()
    fun updateLoftPigeonForUser(bean: ShedRingInfoParams) {
        val imgFileBody = bean.bodyPic
        val imgFileFly = bean.featherPic
        val imgFileLeft = bean.leftHead
        val imgFileRight = bean.rightHeadPic

        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()
        hashMap["id"] = bean.id.toString()
        hashMap["fosterName"] = bean.fosterName
        hashMap["ringId"] = bean.ringId
        hashMap["memberId"] = bean.memberId.toString()
        hashMap["wantedOrgId"] = bean.wantedOrgId.toString()
        hashMap["sex"] = bean.sex.toString()
        hashMap["featherColor"] = bean.featherColor.toString()
        hashMap["eyePattern"] = bean.eyePattern.toString()

        if (imgFileBody != null) {
            fileList["bodyPic"] = imgFileBody
        } else {
            hashMap["bodyPic"] = ""
        }
        if (imgFileFly != null) {
            fileList["featherPic"] = imgFileFly
        } else {
            hashMap["featherPic"] = ""
        }
        if (imgFileLeft != null) {
            fileList["leftHead"] = imgFileLeft
        } else {
            hashMap["leftHead"] = ""
        }
        if (imgFileRight != null) {
            fileList["rightHeadPic"] = imgFileRight
        } else {
            hashMap["rightHeadPic"] = ""
        }
        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.updateLoftPigeonForUser(body) }, {
            val deleteState = UiState(isSuccess = true)
            modifyPigeonPicResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            modifyPigeonPicResult.value = deleteState
        })
    }

    /** 上传探视视频 */
    val addVisitVideoResult: MutableLiveData<Any> = MutableLiveData()
    fun addVisitVideoOss(body: AddVisitVideoResponse) {
        request({ apiService.addVisitVideoOss(body) }, {
            addVisitVideoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

//    fun addVisitVideo(bean: AddVisitVideoResponse) {
//        val videoBody = bean.path
//
//        val fileList = HashMap<String, File?>()
//        val hashMap = HashMap<String, String>()
//        hashMap["pigeonId"] = bean.pigeonId.toString()
//        hashMap["raceId"] = bean.raceId.toString()
//
//        if (videoBody != null) {
//            fileList["path"] = videoBody
//        } else {
//            hashMap["path"] = ""
//        }
//
//        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
//        request({ apiService.addVisitVideo(body) }, {
//            val deleteState = UiState(isSuccess = true)
//            addVisitVideoResult.value = deleteState
//        }, {
//            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
//            addVisitVideoResult.value = deleteState
//        })
//    }

    /** 修改探视视频 */
    val editVisitVideoResult: MutableLiveData<Any> = MutableLiveData()
    fun editVisitVideoOss(body: EditVisitVideoParam) {
        request({ apiService.editVisitVideoOss(body) }, {
            editVisitVideoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
//    fun editVisitVideo(bean: EditVisitVideoParam) {
//        val videoBody = bean.path
//
//        val fileList = HashMap<String, File?>()
//        val hashMap = HashMap<String, String>()
//        hashMap["id"] = bean.id.toString()
//        hashMap["raceId"] = bean.raceId.toString()
//
//        if (videoBody != null) {
//            fileList["path"] = videoBody
//        } else {
//            hashMap["path"] = ""
//        }
//
//        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
//        request({ apiService.editVisitVideo(body) }, {
//            val deleteState = UiState(isSuccess = true)
//            editVisitVideoResult.value = deleteState
//        }, {
//            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
//            editVisitVideoResult.value = deleteState
//        })
//    }

    /** 获取组织下赛事 */
    val orgMatchResult: MutableLiveData<ListDataUiState<OrgMatchResponse.ListInfo>> = MutableLiveData()
    fun getOrgRaceListApp(isRefresh: Boolean, orgId: String) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getOrgRaceListApp(orgId, pageSize, pageNo) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                listData = it.data
            )
            orgMatchResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, isRefresh = isRefresh, errMessage = it.msg, listData = arrayListOf<OrgMatchResponse.ListInfo>()
            )
            orgMatchResult.value = listDataUiState
        })
    }

    /** 收鸽清单 */
    val receiptPigeonResult: MutableLiveData<ListDataUiState<ReceiptPigeonResponse.ListBean>> = MutableLiveData()
    fun getLoftPigeonListByRaceId(isRefresh: Boolean, member: String, ringId: String, raceId: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getLoftPigeonListByRaceId(pageNo, pageSize, member, ringId, raceId) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                hasMore = it.hasMore(),
                total = it.total,
                listData = it.list
            )
            receiptPigeonResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errMessage = it.msg,
                    listData = arrayListOf<ReceiptPigeonResponse.ListBean>()
                )
            receiptPigeonResult.value = listDataUiState
        })
    }

    /** 获取已上传探视视频的赛鸽列表 */
    val getVisitVideoResult: MutableLiveData<ListDataUiState<GetVideoListResponse.ListData>> = MutableLiveData()
    fun getVisitVideoList(isRefresh: Boolean, memberName: String, ringId: String, raceId: Int) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getVisitVideoList(pageNo, pageSize, raceId, memberName, ringId) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                hasMore = it.hasMore(),
                total = it.total,
                listData = it.list
            )
            getVisitVideoResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = isRefresh,
                    errMessage = it.msg,
                    listData = arrayListOf<GetVideoListResponse.ListData>()
                )
            getVisitVideoResult.value = listDataUiState
        })
    }

    /** 获取组织方所有设备 */
    val deviceResult: MutableLiveData<OrganizerDeviceResponse> = MutableLiveData()
    fun getGatherDeviceList() {
        request({ apiService.getGatherDeviceList(pageNo, 999999) }, {
            //请求成功
            deviceResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 删除探视视频 */
    val deleteVideoResult: MutableLiveData<Any> = MutableLiveData()
    fun delVisitVideo(id: String) {
        request({ apiService.delVisitVideo(id) }, {
            //请求成功
            deleteVideoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取单用户下探视棚号列表(打印) */
    val memberNoListResult: MutableLiveData<VisitMemberListResponse> = MutableLiveData()
    fun getVisitMemberNoListII(raceId: Int, userId: Int) {
        request({ apiService.getVisitMemberNoListII(raceId, userId) }, {
            //请求成功
            memberNoListResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 探视打印 */
    val printVisitResult: MutableLiveData<PrintVisitResponse> = MutableLiveData()
    fun getVisitPrint(raceId: Int, memberId: Int) {
        request({ apiService.getVisitPrint(raceId, memberId) }, {
            //请求成功
            printVisitResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 确认打印 */
    val confirmPrintResult: MutableLiveData<UiState> = MutableLiveData()
    fun confirmPrint(body: RequestBody) {
        request({ apiService.confirmPrint(body) }, {
            val deleteState = UiState(isSuccess = true)
            confirmPrintResult.value = deleteState
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            confirmPrintResult.value = result
        })
    }

}